{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7586cd0f",
   "metadata": {},
   "source": [
    "* 学习人：铭晶\n",
    "* 时间：2021-11-25\n",
    "* week 13"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "469dac00",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "## Json（文本数据）--->dict（Python中的字典数据）\n",
    "## 字典和列表的综合实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daa2e4a1",
   "metadata": {},
   "source": [
    "# 本周内容：\n",
    "## 集合\n",
    "> 1.集合的性质     \n",
    ">> 唯一性/不重复（ 去除重复项 : 一般可以用来查看列表的成员属性 ）     \n",
    ">> 无序  \n",
    "\n",
    ">2.集合的一般方法    \n",
    ">> 1.增加     \n",
    ">> 2.删除       \n",
    ">> 3.查询：in 查询成员关系 \n",
    "\n",
    ">3.集合的数学运算逻辑"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51e168aa",
   "metadata": {},
   "source": [
    "### 集合的性质"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29eb503b",
   "metadata": {},
   "source": [
    "# 思考：了解一组数据中有哪些唯一存在的值？（假如是列表）\n",
    "no_duplicate_age=[]\n",
    "list_age=[18,24,25,24,18,23,18]\n",
    "for i in list_age:\n",
    "    if i not in no_duplicate_age:\n",
    "        no_duplicate_age.append(i)\n",
    "no_duplicate_age"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3936d3a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{18, 23, 24, 25}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# list--->set 的方法：set（）\n",
    "no_duplicate_age_set=set(list_age)\n",
    "no_duplicate_age_set\n",
    "# 集合的符号和字典一样的{} 字典的括号加列表的形式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ec15a657",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'apple', 'orange', 'pear', 'banana'}\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 官方文档测试\n",
    "# 1.移除重复项\n",
    "basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}\n",
    "print(basket)\n",
    "# 2.in查询成员关系\n",
    "print('orange' in basket)\n",
    "print('crabgrass' in basket)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17448645",
   "metadata": {},
   "source": [
    "### 集合的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9db91686",
   "metadata": {},
   "source": [
    "* 集合的创建的两种一般方法：\n",
    "> 1.set(list)        \n",
    "> 2.直接按照集合的结构创建：{value1,value2,value3,...}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "492759e3",
   "metadata": {},
   "source": [
    "* 1.增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7747c560",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 4, 5, 6, 77}\n"
     ]
    }
   ],
   "source": [
    "set_1=set([1,2,5,4,6,0,77])\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5599fa7a",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 99, 4, 5, 6, 77}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(99)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f52e196d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 99, 4, 5, 6, 77, 88}\n"
     ]
    }
   ],
   "source": [
    "set_1.add(88)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e88e1ff2",
   "metadata": {},
   "source": [
    "* 2.删除\n",
    "> 1.pop():Remove and return an arbitrary set element.Raises KeyError if the set is empty.  \n",
    "> 2.remove()   \n",
    "> 3.discard()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b06fe9cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 2, 4, 5, 6, 77}\n",
      "{1, 2, 4, 5, 6, 77}\n"
     ]
    }
   ],
   "source": [
    "set_1=set([1,2,5,4,6,0,77])\n",
    "print(set_1)\n",
    "set_1.pop()\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9c4ba8b9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 4, 5, 6, 77}\n",
      "{1, 2, 4, 5, 77}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.remove(6)\n",
    "print(set_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "12ffe6ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "# * 避免KeyError的方式：\n",
    "if 66 in set_1:\n",
    "    set_1.remove(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "725ea32e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 4, 5, 77}\n"
     ]
    }
   ],
   "source": [
    "print(set_1)\n",
    "set_1.discard(6)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c95c0865",
   "metadata": {},
   "source": [
    "### 集合的运算\n",
    "> 1.集合的交集   \n",
    "> 2.集合的并集   \n",
    "> 3.集合的差集 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8116d8ee",
   "metadata": {},
   "source": [
    "* 交集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c99478c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{0, 1, 3, 4, 8, 9, 55}\n"
     ]
    }
   ],
   "source": [
    "set_2=set([1,3,8,4,9,0,55])\n",
    "print(set_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "190e3f9e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 {1, 2, 4, 5, 77}\n",
      "set_2 {0, 1, 3, 4, 8, 9, 55}\n",
      "交集结果： {1, 4}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1\",set_1)\n",
    "print(\"set_2\",set_2)\n",
    "print(\"交集结果：\",set_1.intersection(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "8d3f3da0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 {1, 2, 4, 5, 77}\n",
      "set_2 {0, 1, 3, 4, 8, 9, 55}\n",
      "交集结果： {1, 4}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符：set_1&set_2\n",
    "print(\"set_1\",set_1)\n",
    "print(\"set_2\",set_2)\n",
    "print(\"交集结果：\",set_1&set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28988288",
   "metadata": {},
   "source": [
    "* 并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b6df9211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 {1, 2, 4, 5, 77}\n",
      "set_2 {0, 1, 3, 4, 8, 9, 55}\n",
      "并集结果： {0, 1, 2, 3, 4, 5, 8, 9, 77, 55}\n"
     ]
    }
   ],
   "source": [
    "print(\"set_1\",set_1)\n",
    "print(\"set_2\",set_2)\n",
    "print(\"并集结果：\",set_1.union(set_2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "64a41dee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set_1 {1, 2, 4, 5, 77}\n",
      "set_2 {0, 1, 3, 4, 8, 9, 55}\n",
      "并集结果： {0, 1, 2, 3, 4, 5, 8, 9, 77, 55}\n"
     ]
    }
   ],
   "source": [
    "# 使用逻辑运算符：set_1|set_2\n",
    "print(\"set_1\",set_1)\n",
    "print(\"set_2\",set_2)\n",
    "print(\"并集结果：\",set_1|set_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "566023fc",
   "metadata": {},
   "source": [
    "* 差集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f4726900",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"set_1\",set_1)\n",
    "print(\"set_2\",set_2)\n",
    "print(\"并集结果：\",set_1.union(set_2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
